﻿#region Copyright and Licensing Notice
// Copyright (C) 2011 Steven Bosnick
// 
// This program is open source software; you can redistribute it 
// and/or modifiy it under the terms of the Eclipse Public License,
// version 1.0.
// 
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A 
// PARTICULAR PURPOSE. See the Eclipse Public License for more
// details.
// 
// You should have received a copy of the Eclipse Public License 
// along with this program; if not, you can find a copy at
// "http://www.opensource.org/licenses/eclipse-1.0.php".
#endregion
using System;
using BitwiseOre.Properties;
using BitwiseOre.SharedMemory.Core;
using BitwiseOre.SharedMemory.JetBrains;

namespace BitwiseOre.SharedMemory.Context
{
    [UsedImplicitly]
    internal sealed class StreamContext : IStreamContext, IStreamProperties
    {
        private const int DefaultBufferSize = 2048;
        private const int DefaultChunckingFactor = 4;

        private readonly IConnectionBuffersSizer _sizer;

        private int _bufferSize = DefaultBufferSize;
        private int _chunckingFactor = DefaultChunckingFactor;

        public StreamContext(IConnectionBuffersSizer sizer)
        {
            _sizer = sizer;
        }

        int IStreamContext.BufferSize
        {
            get { return _bufferSize; }
            set
            {
                ValidateContextBufferSize(value);

                _bufferSize = value;
            }
        }

        int IStreamContext.ChunckingFactor
        {
            get { return _chunckingFactor; }
            set
            {
                ValidateContextChunkingFactor(value);

                _chunckingFactor = value;
            }
        }

        int IStreamProperties.BufferCount
        {
            get { return _chunckingFactor; }
        }

        int IStreamProperties.BufferSize
        {
            get { return GetChunkSize(_bufferSize); }
        }

        private void ValidateContextBufferSize(int value)
        {
            string message;
            if (value <= 0)
            {
                message = Resources.StreamContext_BufferSizeTooSmallMessage;
                throw new ArgumentOutOfRangeException("BufferSize", value, message);
            }
            if (_sizer.IsSizeInvalid(_chunckingFactor, GetChunkSize(value)))
            {
                message = Resources.StreamContext_BufferSizeTooLargeMessage;
                throw new ArgumentOutOfRangeException("BufferSize", value, message);
            }
            if (value < _chunckingFactor)
            {
                message = Resources.StreamContext_BufferSizeLessThanChunkingFactorMessage;
                throw new ArgumentException(message, "BufferSize");
            }
        }

        private void ValidateContextChunkingFactor(int value)
        {
            string message;
            if (value <= 0)
            {
                message = Resources.StreamContext_ChunkingFactorTooSmallMessage;
                throw new ArgumentOutOfRangeException("ChunkingFactory", value, message);
            }
            if (value > _bufferSize)
            {
                message = Resources.StreamContext_ChunkingFactorGreaterThanBufferSizeMessage;
                throw new ArgumentException(message,"ChunkingFactor");
            }
        }

        private int GetChunkSize(int bufferSize)
        {
            return bufferSize/_chunckingFactor;
        }
    }
}